package cn.sheep.dolphin.tools

import cn.sheep.dolphin.common.{DolphinAppComm, Jedispools}
import org.apache.commons.lang.StringUtils

/**
  * 字典数据存入到redis中
  * author: old sheep
  * QQ: 64341393 
  * Created 2018/12/1
  */
object Appdict2Redis {

	def main(args: Array[String]): Unit = {

		if (args.size != 1) {
			println(
				"""
				  |Usage: cn.sheep.dolphin.tools.Appdict2Redis <appdictFilePath>
				""".stripMargin)
			sys.exit(-1)
		}

		val Array(appdictFilePath) = args
		val sc = DolphinAppComm.createSparkContext("字典数据存入到redis中")

		sc.textFile(appdictFilePath)
			.map(line => line.split("\t", -1))
			.foreachPartition(partition => {
				val jedis = Jedispools.getJedis()
				// 写入数据
				partition.foreach(arr => {
					// appid 和 appname都不能为空，否则没有意义
					if (StringUtils.isNotEmpty(arr(4)) && StringUtils.isNotEmpty(arr(1))) {
						jedis.hset("appdict", arr(4), arr(1))
					}
				})
				jedis.close()
			})
		sc.stop()
	}

}
